let encryption
// 异步导入
const asyncLoad = () => {
  if (encryption) return false;
  try {
	  return new Promise((resolve)=>{
		  require.ensure([], () => {
		    import('encryption').then((res) => {
				resolve(res.encryption)
		    });
		  });
	  })
    
  } catch (e) {
    uni.showModal({
      title: '错误',
      content: `导入失败：${JSON.stringify(e)}`,
      showCancel: false,
      cancelText: '知道了',
    });
    return Promise.reject(new Error(e));
  }
};

const get_encryption = async ()=>{
	return await asyncLoad()
}


export default async ({ url, data, header = { 'Content-Type': 'application/json' }, method = 'GET', encryptionStatus = false }) => {
	if(!encryption){
		encryption = await get_encryption()
	}
	if(encryptionStatus && encryption){
		data =  JSON.parse(encryption(JSON.stringify(data)))
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			data,
			header,
			method,
			success: (res) => {
				resolve(res)
			},
			fail: (err) => {
				uni.showToast({
					title: '网络请求失败',
					duration: 2000,
					icon: 'none'
				});
				reject(err)
			},
			complete: () => {}
		});
	})
}